﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CarLoanSystem.Entity;

namespace CarLoanSystem.DAL
{
    public class CarBrandInfoDAL : DBBase
    {
        public static CarBrandItem LoadCarBrand(int id)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                SqlCommand command = new SqlCommand(@"SELECT [CAR_BRAND_ID] ,[CAR_BRAND_NAME]
                FROM [CAR_LOAN].[dbo].[CAR_BRAND] where [CAR_BRAND_ID] = @CAR_BRAND_ID", DBBase.con);
                command.Parameters.AddWithValue("@CAR_BRAND_ID", id);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    CarBrandItem item = new CarBrandItem();
                    item.CAR_BRAND_ID = Convert.ToInt32(dt.Rows[0]["CAR_BRAND_ID"].ToString());
                    item.CAR_BRAND_NAME = dt.Rows[0]["CAR_BRAND_NAME"].ToString();
                    return item;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataTable LoadAllCarBrand(string id = "", string brandName = "")
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                string query = @"SELECT [CAR_BRAND_ID] ,[CAR_BRAND_NAME] FROM [CAR_LOAN].[dbo].[CAR_BRAND] where 1=1";
                if (id != "")
                {
                    query += " And CAR_BRAND_ID = " + id;
                }
                if (brandName != "")
                {
                    query += " And CAR_BRAND_NAME like '%" + brandName + "%'";
                }

                SqlCommand command = new SqlCommand(query, DBBase.con);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    return dt;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static DataTable LoadAllForComboBox()
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlDataReader dr;
                string query = @"SELECT [CAR_BRAND_ID] ,[CAR_BRAND_NAME] FROM [CAR_LOAN].[dbo].[CAR_BRAND]";
                
                SqlCommand command = new SqlCommand(query, DBBase.con);
                dr = command.ExecuteReader();
                DataTable dt = new DataTable();
                if (dr.HasRows)
                {
                    dt.Load(dr);
                }
                DBBase.DisConncetDatabase();
                if (dt.Rows.Count > 0)
                {
                    return dt;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static void Insert(CarBrandItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"INSERT INTO [CAR_LOAN].[dbo].[CAR_BRAND]
                                                       ([CAR_BRAND_NAME])
                                                 VALUES
                                                      ( @CAR_BRAND_NAME)", DBBase.con);

                command.Parameters.AddWithValue("@CAR_BRAND_NAME", item.CAR_BRAND_NAME);
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }

        public static bool Update(CarBrandItem item)
        {
            try
            {
                DBBase.ConncetDatabase();
                SqlCommand command = new SqlCommand(@"UPDATE [CAR_LOAN].[dbo].[CAR_BRAND]
                                                        SET [CAR_BRAND_NAME] = @CAR_BRAND_NAME
                                                    WHERE ([CAR_BRAND_ID] = @CAR_BRAND_ID )", DBBase.con);

                command.Parameters.AddWithValue("@CAR_BRAND_NAME", item.CAR_BRAND_NAME);
                command.Parameters.AddWithValue("@CAR_BRAND_ID", item.CAR_BRAND_ID);
                int tmp = command.ExecuteNonQuery();
                DBBase.DisConncetDatabase();
                return true;
            }
            catch (Exception ex)
            {
                DBBase.DisConncetDatabase();
                throw ex;
            }
        }
    }
}
